#include "desktopwidget.h"
#include <QPushButton>
#include <QGridLayout>
#include <QLabel>
#include <QFile>
#include <QLCDNumber>
#include <QTime>
#include <QTimer>
#include <QDebug>
#include "configargs.h"
 
DesktopWidget::DesktopWidget(QWidget *parent) : QWidget(parent)
{

    QPushButton *pWqTestBtn = new QPushButton();
    QPushButton *pPhotoBtn = new QPushButton();
    QPushButton *pWebBtn = new QPushButton();
    QPushButton *pCubeBtn = new QPushButton();
    QPushButton *pAboutBtn = new QPushButton();
    QPushButton *pTimeBtn = new QPushButton();
    QPushButton *pFolderBtn = new QPushButton();
    QLabel *pWqLab = new QLabel();

    m_pTimeLab = new QLCDNumber();

    QGridLayout *pMainLayout = new QGridLayout();
    pMainLayout->setMargin(SUBSPACE);      ////表示控件与窗体的左右边距
    pMainLayout->setSpacing(SUBSPACE);		//表示各个控件之间的上下间距

    pMainLayout->addWidget(pWqTestBtn, 0, 0, 1, 3);
    pMainLayout->addWidget(m_pTimeLab, 0, 3, 1, 2);
    pMainLayout->addWidget(pPhotoBtn, 1, 0, 2, 1);
    pMainLayout->addWidget(pWqLab, 1, 1, 2, 3);
    pMainLayout->addWidget(pFolderBtn, 1, 4, 1, 1);
    pMainLayout->addWidget(pTimeBtn, 2, 4, 1, 1);
    pMainLayout->addWidget(pWebBtn, 3, 0, 1, 1);
    pMainLayout->addWidget(pCubeBtn, 3, 1, 1, 2);
    pMainLayout->addWidget(pAboutBtn, 3, 3, 1, 2);

    pMainLayout->setAlignment(m_pTimeLab, Qt::AlignCenter);

    this->setLayout(pMainLayout);

    pWqTestBtn->setObjectName("toolsBtn");
    pPhotoBtn->setObjectName("photoBtn");
    pWebBtn->setObjectName("ieBtn");
    pCubeBtn->setObjectName("cubeBtn");
    pAboutBtn->setObjectName("aboutBtn");
    pTimeBtn->setObjectName("timeBtn");
    pFolderBtn->setObjectName("folderBtn");
    pWqLab->setObjectName("wqLab");
    m_pTimeLab->setObjectName("timeLab");

    pWqTestBtn->setFocusPolicy(Qt::NoFocus);
    pPhotoBtn->setFocusPolicy(Qt::NoFocus);
    pWebBtn->setFocusPolicy(Qt::NoFocus);
    pCubeBtn->setFocusPolicy(Qt::NoFocus);
    pAboutBtn->setFocusPolicy(Qt::NoFocus);
    pTimeBtn->setFocusPolicy(Qt::NoFocus);
    pFolderBtn->setFocusPolicy(Qt::NoFocus);

    loadQss(); 

    refreshTimeLCD();

    m_pTimer = new QTimer();
    connect(m_pTimer, SIGNAL(timeout()), this, SLOT(refreshTimeLCD()));
    m_pTimer->setInterval(1000);
    m_pTimer->start();

    connect(pWqTestBtn, SIGNAL(clicked(bool)), this, SIGNAL(wqTestBtnClicked()));
    connect(pPhotoBtn, SIGNAL(clicked(bool)), this, SIGNAL(photoBtnClicked()));
    connect(pAboutBtn, SIGNAL(clicked(bool)), this, SIGNAL(aboutBtnClicked()));
    connect(pWebBtn, SIGNAL(clicked(bool)), this, SIGNAL(webBtnClicked()));
    connect(pTimeBtn, SIGNAL(clicked(bool)), this, SIGNAL(timeBtnClicked()));
    connect(pFolderBtn, SIGNAL(clicked(bool)), this, SIGNAL(folderBtnClicked()));
    connect(pCubeBtn, SIGNAL(clicked(bool)), this, SIGNAL(musicBtnClicked()));

}

void DesktopWidget::hideEvent(QHideEvent *event)
{
    if(m_pTimer->isActive()) {
        m_pTimer->stop();
    }
}

void DesktopWidget::showEvent(QShowEvent *event)
{
    refreshTimeLCD();

    if(!m_pTimer->isActive()) {
        m_pTimer->start();
    }
}


void DesktopWidget::refreshTimeLCD()
{
    QString timeStr = QTime::currentTime().toString("hh:mm");
    m_pTimeLab->display(timeStr);
}

void DesktopWidget::loadQss()
{
	QFile iconQss;
	QFile sizeQss;

    QString iconQssStr;
    QString sizeQssStr;

    if(g_lcdsize == LCDSIZE_1280x800){
        iconQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopicon_1280x800.qss");
        sizeQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopsize_1280x800.qss");
        iconQss.open(QFile::ReadOnly);
        sizeQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
        sizeQssStr = sizeQss.readAll();
    }else if(g_lcdsize == LCDSIZE_1024x768){
        iconQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopicon_1024x768.qss");
        sizeQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopsize_1024x768.qss");
        iconQss.open(QFile::ReadOnly);
        sizeQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
        sizeQssStr = sizeQss.readAll();
    }else if(g_lcdsize == LCDSIZE_800x600){
        iconQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopicon_800x600.qss");
        sizeQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopsize_800x600.qss");
        iconQss.open(QFile::ReadOnly);
        sizeQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
        sizeQssStr = sizeQss.readAll();
    }else   if(g_lcdsize == LCDSIZE_800x480){
               
        qDebug("using WIDGET_W_7b in desktopwidget.cpp");
        iconQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopicon7b.qss");
        sizeQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopsize7b.qss");
        iconQss.open(QFile::ReadOnly);
        sizeQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
        sizeQssStr = sizeQss.readAll();

    }else   if(g_lcdsize == LCDSIZE_480x272){
        iconQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopicon_480x272.qss");
        sizeQss.setFileName(":/qss/linuxdesktop/stylesheet/desktopsize_480x272.qss");
        iconQss.open(QFile::ReadOnly);
        sizeQss.open(QFile::ReadOnly);
        iconQssStr = iconQss.readAll();
        sizeQssStr = sizeQss.readAll();
    }

    this->setStyleSheet(sizeQssStr + iconQssStr);
}
